/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entidades.Persona;
import entidades.Usuario;
import entidades.UsuarioSession;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author RENZO
 */
public class UsuarioDAO extends AbstractDAO {
    
    public static Usuario iniciarSesion(String usuario,String password,short tipo){
        Usuario user = null;
        String query = "SELECT * FROM iniciarSesion(cast('"+usuario+"' as varchar),cast('"+password+"' as varchar),cast("+tipo+" as int2))";
        PreparedStatement ps = prepararConexionCall(query);
        try {
            ResultSet rs = realizarConexionGET(ps);
            dataAccess.getConnection().commit();
            if(rs.next()){
                user = new Usuario();
                Persona pers = new Persona();
                pers.setNombre(rs.getString("nombre"));
                pers.setaPaterno(rs.getString("apaterno"));
                pers.setaMaterno(rs.getString("amaterno"));
                pers.setTipo(rs.getShort("tipo"));
                user.setPersona(pers);
                user.setNombreUsuario(rs.getString("usuario"));
                user.setId(rs.getLong("id"));
                UsuarioSession sesion = new UsuarioSession();
                sesion.setSid(rs.getString("sid"));
                sesion.setUsuario(user);
                UsuarioSession.setSesionActual(sesion);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                ps.close();
                dataAccess.closeConnection();
            } catch (SQLException ex) {
                Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
        return user;
    }
    
    public static Boolean validarSesion (UsuarioSession sesion){
        Boolean rpta = false;
        PreparedStatement ps = null;
        try {
            ps = prepararConexionCall("SELECT COUNT(usuariosession_id) FROM usuariosession WHERE usuariosession_sid = '"+sesion.getSid()+
                                      "' AND usuariosession_fechacierre IS NULL ");
            ResultSet rs = realizarConexionGET(ps);
            if(rs.next()){
                if(rs.getInt(1) > 0){
                    rpta = true;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                ps.close();
                dataAccess.closeConnection();
            } catch (SQLException ex) {
                Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return rpta;
    }
    
    public static List<Long>obtenerZonasPermitidas(long usuarioId){
        List<Long> listaZonas = new ArrayList<Long>();
        PreparedStatement ps = null;
        try {
            ps = prepararConexionCall(" SELECT zona_id from usuariozona where usuario_id = "+ String.valueOf(usuarioId));
            ResultSet rs = realizarConexionGET(ps);
            while(rs.next()){
                    listaZonas.add(rs.getLong(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                ps.close();
                dataAccess.closeConnection();
            } catch (SQLException ex) {
                Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return listaZonas;
    }
}
